import time
from datetime import datetime
import re

from cx_Oracle import IntegrityError

from crawl.utils import save_score, SKU_DETAIL_ID, review_split, save_review, c, conn, close_db, update_score, max_date, \
    newReview, logger, sql_commit, log_info
from crawl.unchange.earphone.earphone_review import EarphoneReview


class EarphoneNewReview(EarphoneReview):

    def parse_score(self, url):
        # sku_id
        try:
            self.sku_id = re.search(r"shopdetail/(\d+)", url).group(1)
        except:
            return True
        data = {"webno": self.sku_id}
        try:
            html = self.parse_url(self.url_star, data)
        except Exception as e:
            return True
        self.max_date = max_date(self.SKU_DETAIL_ID)
        # 总评分
        try:
            total_score = html.xpath(
                "//li[@class='detail_status_level_top']//div[@class='detail_status_level_point']/p/text()")
            total_score = float(total_score[0])
        except Exception as e:
            total_score = 0
        # 更新评分
        update_score(total_score, self.sku_id, self.name, self.SKU_DETAIL_ID, conn)

    def parse_review(self):
        data = {"webno": self.sku_id, "fstaff": 0}
        try:
            html = self.parse_url(self.url_review, data)
        except Exception as e:
            return True
        divs = html.xpath("//div[@class='cnt-review']")
        if len(divs) < 1:
            print(self.sku_id+"评论主标签获取失败")
            return True
        for div in divs:
            try:
                # 评论ID
                review_id = self.SKU_DETAIL_ID + "_" + div.xpath("./div[@class='review-right']/span[@class='cnt-su']/@id")[0]
                # 评论星级
                score = div.xpath(".//div[@class='star-det']//div[@class='h-point']/text()")[0]
                # 评论人
                name = div.xpath(".//div[@class='name']/text()")[0]
                # 评论时间  [2019/05/02 23:27]
                date = div.xpath(".//div[@class='day']/text()")[0]
                try:
                    date = re.search(r"\[(.*)\]", date).group(1).split(" ")[0]
                except:
                    continue
                re_date = datetime.strptime(date, "%Y/%m/%d")
                if newReview(self.max_date, re_date):
                    return True
                # 评论标题
                title = div.xpath(".//div[@class='title']/text()")[0]
                # 评论内容
                text = div.xpath("./div[@class='review-right']/div[@class='review']/div/text()")[0].replace("\n", "\t")
            except Exception as e:
                continue
            REVIEW_TEXT1, REVIEW_TEXT2, REVIEW_TEXT3, REVIEW_TEXT4, REVIEW_TEXT5 = review_split(
                text.replace("\n", "\t"))
            REVIEW_TEXT5 += "  from_Bicc"
            sql = save_review(review_id, self.sku_id, score, name, title, REVIEW_TEXT1, REVIEW_TEXT2, REVIEW_TEXT3, REVIEW_TEXT4, date, REVIEW_TEXT5, self.SKU_DETAIL_ID)
            try:
                c.execute(sql)
                conn.commit()
                self.num += 1
            except IntegrityError:
                conn.rollback()
            except UnicodeEncodeError:
                conn.rollback()
            except Exception as e:
                logger(self.name, self.sku_id, e)
                conn.rollback()

def main(urls):
    if len(urls) < 1 or isinstance(urls, list) is False:
        return True
    for url in urls:
        ear = EarphoneNewReview()
        ear.run(url)
